<?php

Yii::import('Cms.controllers.MetadataContentController');
class PinterestController extends MetadataContentController implements ICmsWidget
{
    public $info = array(
        'path'=>'Cms/widgets/pinterest',
        'name'=>'Pinterest Widget',
        'description'=>'Pinterest Widget',
        'images'=>'',
        'video'=>'',
        'author'=>'',
        'version'=>1,
    );
    
    public function getDefaultConfig()
    {
        return CMap::mergeArray(parent::getDefaultConfig(), array(
            'id'=>array(
                'label'=>'Pinterest id',
                'value'=>0,
                'description'=>'Pinterest id',
                'setting_group'=>'General Settings',
                'visible'=>false,
            ),
            'image'=>array(
                'label'=>'URL of the image',
                'value'=>'',
                'description'=>'URL of the image to be pinned',
                'setting_group'=>'General Settings',
                'visible'=>false,
            ),
            'url'=>array(
                'label'=>'URL of the page',
                'value'=>'',
                'description'=>'URL of the page the pin is on',
                'setting_group'=>'General Settings',
                'visible'=>true,
            ),
            'description'=>array(
                'label'=>'Description',
                'value'=>'',
                'description'=>"Description of what they're pinning",
                'setting_group'=>'General Settings',
                'type'=>'textarea',
                'htmlOptions'=>array('rows'=>7,'cols'=>70),
                'visible'=>true,
            ),
        ));
    }
    
    public function __construct($id,$module=null)
    {
        //new instance of widget => load widget settings
        $this->getWidgetSettings();
        parent::__construct($id,$module);
    }
    
    public function actionInstall()
    {
        $this->installWidget();
    }
    
    public function actionUninstall()
    {
        $this->uninstallWidget();
    }
    
    public function actionConfig()
    {
        $this->widgetConfig();
    }
    
    public function actionIndex()
    {
        $this->render($this->getWidgetLayout()->getViewFile());
    }
    
    public function actionUpdate()
    {
        $formParams = $this->getFormParams();
        $formLayoutParams = $this->getFormLayoutParams();
        
        $widgetSetting = array();
        if (Yii::app()->request->IsPostRequest) {
            $this->message = Yii::t('Core','Item has been saved successfully.');
            //save widget settings
            $widgetSetting['widget'] = $formParams->saveParams($_POST, ParamForm::TO_ARRAY);
            //save widget layout settings
            $widgetSetting['layout'] = $formLayoutParams->saveParams($_POST, ParamForm::TO_ARRAY);
            if (parent::updateWidgetSettings($widgetSetting))
            {
                if (isset($_POST['continue']))
                    $this->refresh();
                else
                {
                    //close fancybox, reload page
                    Yii::app()->clientScript->registerScript('ReloadPage', "parent.window.location = parent.window.location;");
                }
            }
        }
        
        $this->render('form', array(
            'formParams' => $formParams,
            'formLayoutParams' => $formLayoutParams,
        ));
    }
}
